/*
 * Copyright 2002-2006 the original author or authors. Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License. You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 * either express or implied. See the License for the specific language governing permissions and limitations under the
 * License.
 */
package pikes.html.xhtml;

import pikes.xml.XMLTag;
import pikes.core.Printable;
import pikes.core.PrintableCharSequence;

public aspect EventsAspect {

    private EventsImplementation Events.eventsDelegate = new EventsImplementation();
    
    public void Events.setOnClick(CharSequence event) {
        eventsDelegate.setOnClick(new PrintableCharSequence(event));
    }
    
    public void Events.setOnDblClick(CharSequence event) {
        eventsDelegate.setOnDblClick(new PrintableCharSequence(event));
    }
    
    public void Events.setOnMouseDown(CharSequence event) {
        eventsDelegate.setOnMouseDown(new PrintableCharSequence(event));
    }
    
    public void Events.setOnMouseUp(CharSequence event) {
        eventsDelegate.setOnMouseUp(new PrintableCharSequence(event));
    }
    
    public void Events.setOnMouseOver(CharSequence event) {
        eventsDelegate.setOnMouseOver(new PrintableCharSequence(event));
    }
    
    public void Events.setOnMouseMove(CharSequence event) {
        eventsDelegate.setOnMouseMove(new PrintableCharSequence(event));
    }
    
    public void Events.setOnMouseOut(CharSequence event) {
        eventsDelegate.setOnMouseOut(new PrintableCharSequence(event));
    }
    
    public void Events.setOnKeyPress(CharSequence event) {
        eventsDelegate.setOnKeyPress(new PrintableCharSequence(event));
    }
    
    public void Events.setOnKeyDown(CharSequence event) {
        eventsDelegate.setOnKeyDown(new PrintableCharSequence(event));
    }
    
    public void Events.setOnKeyUp(CharSequence event) {
        eventsDelegate.setOnKeyUp(new PrintableCharSequence(event));
    }
    
    public void Events.setOnClick(Printable event) {
        eventsDelegate.setOnClick(event);
    }
    
    public void Events.setOnDblClick(Printable event) {
        eventsDelegate.setOnDblClick(event);
    }
    
    public void Events.setOnMouseDown(Printable event) {
        eventsDelegate.setOnMouseDown(event);
    }
    
    public void Events.setOnMouseUp(Printable event) {
        eventsDelegate.setOnMouseUp(event);
    }
    
    public void Events.setOnMouseOver(Printable event) {
        eventsDelegate.setOnMouseOver(event);
    }
    
    public void Events.setOnMouseMove(Printable event) {
        eventsDelegate.setOnMouseMove(event);
    }
    
    public void Events.setOnMouseOut(Printable event) {
        eventsDelegate.setOnMouseOut(event);
    }
    
    public void Events.setOnKeyPress(Printable event) {
        eventsDelegate.setOnKeyPress(event);
    }
    
    public void Events.setOnKeyDown(Printable event) {
        eventsDelegate.setOnKeyDown(event);
    }
    
    public void Events.setOnKeyUp(Printable event) {
        eventsDelegate.setOnKeyUp(event);
    }
    
    pointcut decorateTag(Events events) : execution( protected void TagFactory.decorateCreatedTag(..)) && this(events) && !within(EventsImplementation);
    
    after(Events events) : decorateTag(events) {
        XMLTag tag = (XMLTag)thisJoinPoint.getArgs()[0];
        
        events.eventsDelegate.decorateTag(tag);
        
    }
}

class EventsImplementation implements TagDecorator {
    
    private Printable onClick = null;
    
    private Printable onDblClick = null;
    
    private Printable onMouseDown = null;
    
    private Printable onMouseUp = null;
    
    private Printable onMouseOver = null;
    
    private Printable onMouseMove = null;
    
    private Printable onMouseOut = null;
    
    private Printable onKeyPress = null;
    
    private Printable onKeyDown = null;
    
    private Printable onKeyUp = null;

    public void decorateTag(XMLTag tag) {
        if (onClick != null) {
            tag.addAttribute("onclick", onClick);
        }

        if (onDblClick != null) {
            tag.addAttribute("ondblclick", onDblClick);
        }
        
        if (onMouseDown != null) {
            tag.addAttribute("onmousedown", onMouseDown);
        }

        if (onMouseUp != null) {
            tag.addAttribute("onmouseup", onMouseUp);
        }
        
        if (onMouseOver != null) {
            tag.addAttribute("onmouseover", onMouseOver);
        }
        
        if (onMouseMove != null) {
            tag.addAttribute("onmousemove", onMouseMove);
        }
        
        if (onMouseOut != null) {
            tag.addAttribute("onmouseout", onMouseOut);
        }
        
        if (onKeyPress != null) {
            tag.addAttribute("onkeypress", onKeyPress);
        }
        
        if (onKeyDown != null) {
            tag.addAttribute("onkeydown", onKeyDown);
        }
        
        if (onKeyUp != null) {
            tag.addAttribute("onkeyup", onKeyUp);
        }
    }
    
    public void setOnClick(Printable onClick) {
        this.onClick = onClick;
    }

    public void setOnDblClick(Printable onDblClick) {
        this.onDblClick = onDblClick;
    }

    public void setOnKeyDown(Printable onKeyDown) {
        this.onKeyDown = onKeyDown;
    }

    public void setOnKeyPress(Printable onKeyPress) {
        this.onKeyPress = onKeyPress;
    }

    public void setOnKeyUp(Printable onKeyUp) {
        this.onKeyUp = onKeyUp;
    }

    public void setOnMouseDown(Printable onMouseDown) {
        this.onMouseDown = onMouseDown;
    }

    public void setOnMouseMove(Printable onMouseMove) {
        this.onMouseMove = onMouseMove;
    }

    public void setOnMouseOut(Printable onMouseOut) {
        this.onMouseOut = onMouseOut;
    }

    public void setOnMouseOver(Printable onMouseOver) {
        this.onMouseOver = onMouseOver;
    }

    public void setOnMouseUp(Printable onMouseUp) {
        this.onMouseUp = onMouseUp;
    }
}